package com.dk.jwzx.reptile.subject;


import com.dk.jwzx.bean.POLICY;
import com.dk.jwzx.bean.Subject;
import com.dk.jwzx.bean.Teacher;
import com.dk.jwzx.bean.Week;
import com.dk.jwzx.reptile.ReptileBase;
import com.dk.util.ParseUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 利用jsoup实现doCrawling
 */
public class CquptSubjectReptile extends ReptileBase {

    @Override
    public Map<Integer, List<Subject>> doCrawling(String html) {
        Document document=Jsoup.parse(html);
        Element kbList=document.getElementById("kbStuTabs-list");
        Elements kb=kbList.getElementsByTag("tbody");
        Element tbody=kb.first();
        Elements trs=tbody.getElementsByTag("tr");

        List<Subject> allSubjects=new ArrayList<>();
        for(int i=0;i< trs.size();){

            Element tr=trs.get(i);
            int rowspan= ParseUtil.stringParseInt(tr.child(2).attr("rowspan"));
            //System.out.println(rowspan);
            Subject subject=resolver(tr);
            allSubjects.add(subject);
            //如果课程有不同时间安排
            i++;
            for(int j=1;j<rowspan;j++,i++){
                allSubjects.add(resolverMulti(subject,trs.get(i)));
            }
            //break;
        }

        return wrap(allSubjects);
    }

    private Subject resolver(Element element){
        Subject subject=new Subject();
        String[] strings=element.text().split(" ");
        subject.setClassId(strings[2]);
        subject.setClassName(strings[1].split("-",2)[1]);
        subject.setClassAddress(strings[8]);
        subject.setDay(ParseUtil.charParseInt(strings[6].charAt(2)));
        //设置第几节课
        String time=strings[6].substring(4).split("节")[0];
        setTime(subject,time);

        Teacher teacher=new Teacher();
        teacher.setName(strings[5]);
        subject.setTeacher(teacher);
        //----设置周次
        String week=strings[7].split("周")[0];
        setWeek(subject,week);
        setPolicy(subject.getWeek(), strings[7]);

        subject.setBlank(false);

        return subject;
    }

    private Subject resolverMulti(Subject father,Element element){
        Subject subject=new Subject();
        String[] strings=element.text().split(" ");
        subject.setClassId(father.getClassId());
        subject.setClassName(father.getClassName());
        subject.setClassAddress(strings[3]);
        subject.setDay(ParseUtil.charParseInt(strings[1].charAt(2)));
        //设置第几节课
        String time=strings[2].split("节")[0].substring(1);
        setTime(subject,time);

        Teacher teacher=new Teacher();
        teacher.setName(strings[0]);
        subject.setTeacher(teacher);
        //设置周次
        String week=strings[2].substring(5).split("周")[0];
        setWeek(subject,week);
        setPolicy(subject.getWeek(), strings[2]);
        return subject;
    }

    private void setTime(Subject subject,String str){
        if(str.contains("-")){
            String[] t=str.split("-");
            subject.setTime(new int[]{
                    ParseUtil.stringParseInt(t[0]),
                    ParseUtil.stringParseInt(t[1])
            });
        }else{
            subject.setTime(new int[]{
                    ParseUtil.stringParseInt(str),
                    ParseUtil.stringParseInt(str)
            });
        }
    }

    private void setWeek(Subject subject,String str){
        Week week=new Week();
        if(str.contains("-")){
            String[] w=str.split("-");
            week.setBegin(ParseUtil.stringParseInt(w[0]));
            week.setEnd(ParseUtil.stringParseInt(w[1]));
        }else{
            week.setBegin(ParseUtil.stringParseInt(str));
            week.setEnd(ParseUtil.stringParseInt(str));
        }
        subject.setWeek(week);
    }

    private void setPolicy(Week week,String s){
        if(s.contains("双")){
            week.setPolicy(POLICY.EVEN_NUMBER);
        }else if(s.contains("单")){
            week.setPolicy(POLICY.ODD_NUMBER);
        }else{
            week.setPolicy(POLICY.DEFAULT);
        }
    }

    private Map<Integer,List<Subject>> wrap(List<Subject> allSubjects){
        Map<Integer,List<Subject>> subjectMap=new HashMap<>();

        for(Subject subject:allSubjects){
            Week week= subject.getWeek();
            for(int i= week.getBegin();i<= week.getEnd();){
                if(subjectMap.containsKey(i)){
                    subjectMap.get(i).add(subject);
                }else{
                    List<Subject> list=new ArrayList<>();
                    list.add(subject);
                    subjectMap.put(i,list);
                }

                if(POLICY.DEFAULT.equals(week.getPolicy())){
                    i++;
                }else{
                    //单双周
                    i+=2;
                }
            }
        }

        return subjectMap;
    }
}
